var assert = require('assert');
var uuid = require("node-uuid");
var util = require("util");
var _ = require("underscore");
var Q = require("q");
var config = require("../lib/config.js");
var report = require("../lib/report.js");

config.getPagedResult(1,999,"", function (err, rows) {
    assert.ok(rows != undefined, "获取配置分页失败！");
    assert.ok(rows.length > 0, "获取配置分页失败！");

    //
    //Flatten
    //
    var newConfig = _.clone(rows[0]);
    newConfig.Id = uuid.v1();
    newConfig.Key = newConfig.Key + "Flatten_Test_Modi";
    newConfig.Value = "Flatten_Test_Value";
    var saveOrUpdatePromise = function (newConfig) {
        var deferred = Q.defer();
        config.saveOrUpdate(newConfig, deferred.resolve)
        return deferred.promise;
    }

    Q.nfcall(config.getById, rows[0].Id)
        .then(function (row1) {
            assert.ok(row1 != undefined, "getById获取配置失败！");
            assert.strictEqual(row1.Id, rows[0].Id, "getById获取配置错误！");

            return Q.nfcall(config.getByKey, rows[0].Key);
        })
        .then(function (row1) {
            assert.ok(row1 != undefined, "getByKey获取配置失败！");
            assert.strictEqual(row1.Id, rows[0].Id, "getByKey获取配置错误！");

            return Q.nfcall(config.getByCategoryKey, row1.Category, rows[0].Key);
        })
        .then(function (row1) {
            assert.ok(row1 != undefined, "getByCategoryKey获取配置失败！");
            assert.strictEqual(row1.Id, rows[0].Id, "getByCategoryKey获取配置错误！");

            return Q.nfcall(config.getByCategoryKeyOrDefault, rows[0].Category, rows[0].Key, "");
        })
        .then(function (row1) {
            assert.ok(row1 != undefined, "getByCategoryKeyOrDefault获取配置失败！");
            assert.strictEqual(row1.Id, rows[0].Id, "getByCategoryKeyOrDefault获取配置错误！");

            var uid = uuid.v1();
            return Q.nfcall(config.getByCategoryKeyOrDefault, "", uid, "12345");
        })
        .then(function (row1) {
            assert.ok(row1 != undefined, "getByCategoryKeyOrDefault获取配置失败！");
            assert.strictEqual(row1.Value, "12345", "getByCategoryKeyOrDefault获取配置错误！");

            newConfig.ChangedBy = "saveOrUpdatePromise";
            newConfig.ChangedTS = new Date();
            return saveOrUpdatePromise(newConfig);
        })
        .then(function (row) {
            return Q.nfcall(config.getById, newConfig.Id);
        })
        .then(function (row) {
            assert.ok(row != undefined, "保存配置失败！");
            assert.strictEqual(row.Value, newConfig.Value, "保存配置错误！");

            newConfig.Value = "Test_Value_Update";
            return Q.nfcall(config.saveOrUpdate, newConfig);
        })
        .then(function (row) {
            return Q.nfcall(config.getById, newConfig.Id);
        })
        .then(function (row) {
            assert.ok(row != undefined, "保存配置失败！");
            assert.strictEqual(row.Value, newConfig.Value, "保存配置错误！");

            return Q.nfcall(config.delete, newConfig.Id);
        })
        .then(function (row) {
            return Q.nfcall(config.getById, newConfig.Id);
        })
        .then(function (row) {
            assert.ok(row == undefined, "删除配置失败！");
        })
        .done();
});

config.getPagedResult(1,999,"", function (err, rows) {
    assert.ok(rows != undefined, "获取配置分页失败！");
    assert.ok(rows.length > 0, "获取配置分页失败！");

    //
    //Pyramid of Doom
    //
    var newConfig = _.clone(rows[0]);
    newConfig.Id = uuid.v1();
    newConfig.Key = newConfig.Key + "Pyramid_Test_Modi";
    newConfig.Value = "Pyramid_Test_Value";
    config.saveOrUpdate(newConfig, function (err, row) {
        config.getById(newConfig.Id, function (err, row) {
            assert.ok(row != undefined, "保存配置失败！");
            assert.strictEqual(row.Value, newConfig.Value, "保存配置错误！")

            newConfig.Value = "Test_Value_Update";
            config.saveOrUpdate(newConfig, function (err, row) {
                config.getById(newConfig.Id, function (err, row) {
                    assert.ok(row != undefined, "保存配置失败！");
                    assert.strictEqual(row.Value, newConfig.Value, "保存配置错误！")

                    config.delete(newConfig.Id, function (err, row) {
                        config.getById(newConfig.Id, function (err, row) {
                            assert.ok(row == undefined, "删除配置失败！");
                        });
                    });
                });
            });
        });
    });
});

report.getById("55bb4410-27d2-4323-a45f-a30900ec65db")
    .then(function(data) {
        assert.ok(data != undefined, "获取报表失败！");
        assert.strictEqual(data.Id, "55bb4410-27d2-4323-a45f-a30900ec65db", "获取报表错误 !");
    })
    .done();
